package object;

import java.util.*;

public class MyMap {
    public static void main(String[] args) {
        //Map<String,String> map = new HashMap<>();
        Map<String,String> map1 = new TreeMap<>();
        map1.put("及时雨", "宋江");
        map1.put("黑素芬", "李坤");
        map1.put("及时雨", "李华");
        map1.put("红楼梦", "曹雪芹");
        map1.put("西游记","曹雪芹");
        System.out.println(map1);

        System.out.println(map1.get("西游记"));
        System.out.println(map1.get("李华"));
        //这个方法是高效的
        System.out.println(map1.containsKey("西游记"));
        System.out.println(map1.containsKey("你好"));
        //低效方法,尽量不要用
        System.out.println(map1.containsValue("曹雪芹"));
        //慎用,特别是数据量大的时候
        System.out.println(map1.getOrDefault("及时雨","梁山好汉"));
        System.out.println(map1.getOrDefault("你好","梁山好汉"));

        //这个方法可以拿到所有的key，在数据量大时,慎用
        Set<String> set = map1.keySet();
        System.out.println(set);

        //这个方法可以拿到所有的value，在数据量大时,慎用
        Collection<String> set1 = map1.values();
        System.out.println(set1);

        //map没有实现collection接口,不能使用迭代器/for-each
        //打印key
        for (String s : map1.keySet()) {
            System.out.print(s + " ");
        }
        System.out.println();
        //打印value
        for (String s : map1.values()) {
            System.out.print(s + " ");
        }
        System.out.println();
        //想要同时打印key和value,需要使用entry
        //和keySet一样,慎用
        for (Map.Entry<String,String> entry : map1.entrySet()) {
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }

        map1.remove("及时雨");
        System.out.println(map1);

    }
}
